package org.soretro.portfolio.view
{
	import com.google.analytics.AnalyticsTracker;
	import com.google.analytics.GATracker;
	
	import flash.events.MouseEvent;
	import flash.net.LocalConnection;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.managers.SystemManager;
	
	import org.robotlegs.mvcs.Mediator;
	import org.soretro.portfolio.controller.events.WorkItemEvent;
	import org.soretro.portfolio.helpers.GoogleAnalytics;
	import org.soretro.portfolio.view.videoPlayer.events.VideoPlayerEvent;
	
	import spark.components.VideoPlayer;
	
	public class WorkItemMediator extends Mediator
	{
		[Inject]
		public var view:WorkItemRenderer;
		
		public var youTubeId:String;
		public var tracker:AnalyticsTracker;
		
		override public function onRegister():void
		{
			googleTracker();
			view.addEventListener( MouseEvent.CLICK, onClick );
		}
		
		protected function onClick(event:MouseEvent):void
		{
			youTubeId = view.youTubeCode.text;
			
			var lc:LocalConnection = new LocalConnection();
			
			if ( lc.domain.toString() == "localhost" && event.shiftKey ) {
				var url:String = "http://www.youtube.com/my_videos_edit?ns=1&video_id="+ youTubeId +"&next=%2Fmy_videos";
				var request:URLRequest = new URLRequest(url);
				
				try {
					navigateToURL(request, '_youTubeEditor');
				} catch (e:Error) {
					trace("YouTube editor error occurred!");
				}
			} else {
				trace(">>>>  "+ "YouTube/" + view.titleText.toLowerCase() +"/" + youTubeId );
				GoogleAnalytics.getInstance().send( "YouTube/" + view.titleText.toLowerCase() +"/" + youTubeId );
//				tracker.trackPageview( "YouTube/" + view.titleText.toLowerCase() +"/" + youTubeId );
				dispatch( new VideoPlayerEvent( VideoPlayerEvent.SHOW_VIDEO, youTubeId, view.title.text, view.fullText.text ) );
			}
		}
		
		private function googleTracker():void
		{
			tracker = new GATracker( SystemManager.getSWFRoot(this).stage, "UA-18699334-1", "AS3", false );
		}
	}
}